package com.gpframework.module.process.service.impl;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.lang.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gpframework.common.enums.YesNoEnum;
import com.gpframework.common.utils.DateUtils;
import com.gpframework.common.utils.StringUtils;
import com.gpframework.module.manufacture.service.IGpProductionApplicationService;
import com.gpframework.module.manufacture.service.IGpWeightReductionRuleSettingService;
import com.gpframework.module.module.domain.GpIncomingInspect;
import com.gpframework.module.module.domain.GpIncomingInspectWeight;
import com.gpframework.module.module.domain.GpIncomingInspectWidth;
import com.gpframework.module.module.domain.GpIncomingInspectYardVerify;
import com.gpframework.module.module.domain.req.GpIncomingInspectUpdate;
import com.gpframework.module.module.service.IGpFormaldehydeContrastService;
import com.gpframework.module.process.domain.*;
import com.gpframework.module.process.domain.req.GpProcessDeliveryNoteUpdate;
import com.gpframework.module.process.domain.req.GpProcessIncomingInspectUpdate;
import com.gpframework.module.process.service.*;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.gpframework.module.process.mapper.GpProcessIncomingInspectMapper;
import com.gpframework.module.process.domain.vo.GpProcessIncomingInspectVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
 * @ClassName GpProcessIncomingInspect
 * @Author gpframework
 * @Date 2023-12-20
 * @Version 1.0.0
 * @Description 加工来料检验单Service业务层处理
 */
@Service
public class GpProcessIncomingInspectServiceImpl extends ServiceImpl<GpProcessIncomingInspectMapper, GpProcessIncomingInspect> implements IGpProcessIncomingInspectService {

    @Autowired
    private IGpProcessFormaldehydeContrastService iGpProcessFormaldehydeContrastService;

    @Autowired
    private IGpProcessDeliveryNoteService iGpProcessDeliveryNoteService;

    @Autowired
    private IGpInspectionTableService iGpInspectionTableService;

    @Autowired
    private IGpRevisionTableService iGpRevisionTableService;

    @Autowired
    private IGpProcessProblemTrackService iGpProcessProblemTrackService;

    @Autowired
    private IGpProcessSchedulingService iGpProcessSchedulingService;

    @Autowired
    private IGpProcessIncomingInspectWidthService iGpProcessIncomingInspectWidthService;

    @Autowired
    private IGpProcessIncomingInspectYardVerifyService iGpProcessIncomingInspectYardVerifyService;
    
    @Autowired
    private IGpProcessIncomingInspectFormaldehydeService iGpProcessIncomingInspectFormaldehydeService;

    /**
     * @Description 查询列表返回VO 用于返回给前端的列表接口
     * @Author gpframework
     * @Date 2023-12-20
     * @Param [lqw]
     * @Return List<GpProcessIncomingInspectVo>
     */
    @Override
    public List<GpProcessIncomingInspectVo> listVo(QueryWrapper<GpProcessIncomingInspect> lqw) {
        List<GpProcessIncomingInspectVo> gpProcessIncomingInspectVos = this.baseMapper.listVo(lqw);
        for (GpProcessIncomingInspectVo gpProcessIncomingInspectVo : gpProcessIncomingInspectVos) {
            setSubList(gpProcessIncomingInspectVo);
        }

        return gpProcessIncomingInspectVos;
    }

    private void setSubList(GpProcessIncomingInspectVo gpProcessIncomingInspectVo) {
        gpProcessIncomingInspectVo.setGpProcessIncomingInspectWidthList(iGpProcessIncomingInspectWidthService.list(new QueryWrapper<GpProcessIncomingInspectWidth>().eq("incoming_inspect_id", gpProcessIncomingInspectVo.getId()).eq("del_status", YesNoEnum.NO.getKey()).orderByAsc("create_time")));
        gpProcessIncomingInspectVo.setGpProcessIncomingInspectYardVerifyList(iGpProcessIncomingInspectYardVerifyService.list(new QueryWrapper<GpProcessIncomingInspectYardVerify>().eq("incoming_inspect_id", gpProcessIncomingInspectVo.getId()).eq("del_status", YesNoEnum.NO.getKey()).orderByAsc("create_time")));
        gpProcessIncomingInspectVo.setGpProcessIncomingInspectFormaldehydeList(iGpProcessIncomingInspectFormaldehydeService.list(new QueryWrapper<GpProcessIncomingInspectFormaldehyde>().eq("incoming_inspect_id", gpProcessIncomingInspectVo.getId()).eq("del_status", YesNoEnum.NO.getKey()).orderByAsc("create_time")));
    }

    /**
     * @Description 通过查询详情VO 用于返回给前端的列详情接口
     * @Author gpframework
     * @Date 2023-12-20
     * @Param [id]
     * @Return GpProcessIncomingInspectVo
     */
    @Override
    public GpProcessIncomingInspectVo getVoById(Long id) {
        GpProcessIncomingInspectVo gpProcessIncomingInspectVo = this.baseMapper.getVo(id);
        setSubList(gpProcessIncomingInspectVo);
        return gpProcessIncomingInspectVo;
    }

    @Override
    public boolean createFormaldehyde(GpProcessIncomingInspectUpdate gpProcessIncomingInspectUpdate) {
        ArrayList<GpProcessFormaldehydeContrast> gpProcessFormaldehydeContrasts = new ArrayList<>();
        List<Long> ids = gpProcessIncomingInspectUpdate.getIds();
        for (Long id : ids) {
            GpProcessIncomingInspect gpProcessIncomingInspect = this.getById(id);
            GpProcessFormaldehydeContrast gpProcessFormaldehydeContrast = new GpProcessFormaldehydeContrast();

            if (StringUtils.isNotBlank(gpProcessIncomingInspectUpdate.getCreateBy())) {
                gpProcessFormaldehydeContrast.setCreateBy(gpProcessIncomingInspectUpdate.getCreateBy());
            }
            gpProcessFormaldehydeContrast.setIncomingInspectId(gpProcessIncomingInspect.getId());

            gpProcessFormaldehydeContrast.setSn(gpProcessIncomingInspect.getSn());
            gpProcessFormaldehydeContrast.setCheckTime(new Date());
            gpProcessFormaldehydeContrast.setBatchNo(gpProcessIncomingInspect.getBatchNo());
            gpProcessFormaldehydeContrast.setProductName(gpProcessIncomingInspect.getProductName());
            gpProcessFormaldehydeContrast.setProductColor(gpProcessIncomingInspect.getProductColor());
            gpProcessFormaldehydeContrasts.add(gpProcessFormaldehydeContrast);
        }
        return iGpProcessFormaldehydeContrastService.saveBatch(gpProcessFormaldehydeContrasts);
    }


    @Override
    public boolean createChangeLabel(GpProcessIncomingInspectUpdate gpProcessIncomingInspectUpdate) {
        ArrayList<GpRevisionTable> gpRevisionTables = new ArrayList<>();
        List<Long> ids = gpProcessIncomingInspectUpdate.getIds();
        for (Long id : ids) {
            GpProcessIncomingInspect gpProcessIncomingInspect = this.getById(id);
            GpRevisionTable gpRevisionTable = new GpRevisionTable();


            if (StringUtils.isNotBlank(gpProcessIncomingInspectUpdate.getCreateBy())) {
                gpRevisionTable.setCreateBy(gpProcessIncomingInspectUpdate.getCreateBy());
            }
            gpRevisionTable.setIncomingInspectId(gpProcessIncomingInspect.getId());
            gpRevisionTable.setIncomingId(gpProcessIncomingInspect.getProcessDeliveryNoteId());

            gpRevisionTable.setSn(gpProcessIncomingInspect.getSn());

            gpRevisionTable.setBatchNo(gpProcessIncomingInspect.getBatchNo());
            gpRevisionTable.setProductName(gpProcessIncomingInspect.getProductName());
            gpRevisionTable.setProductColor(gpProcessIncomingInspect.getProductColor());
            GpProcessDeliveryNote gpProcessDeliveryNote = iGpProcessDeliveryNoteService.getById(gpProcessIncomingInspect.getProcessDeliveryNoteId());

            if (gpProcessDeliveryNote!=null){
                gpRevisionTable.setReturnNum(gpProcessDeliveryNote.getNumberItems());
            }
            gpRevisionTables.add(gpRevisionTable);
        }
        return iGpRevisionTableService.saveBatch(gpRevisionTables);
    }

    @Override
    public boolean createSendTest(GpProcessIncomingInspectUpdate gpProcessIncomingInspectUpdate) {
        ArrayList<GpInspectionTable> gpInspectionTables = new ArrayList<>();
        List<Long> ids = gpProcessIncomingInspectUpdate.getIds();
        for (Long id : ids) {
            GpInspectionTable gpInspectionTable = new GpInspectionTable();
            GpProcessIncomingInspect processIncomingInspect = this.getById(id);
            if (StringUtils.isNotBlank(gpProcessIncomingInspectUpdate.getCreateBy())) {
                gpInspectionTable.setCreateBy(gpProcessIncomingInspectUpdate.getCreateBy());
            }
            gpInspectionTable.setSn(processIncomingInspect.getSn());
            gpInspectionTable.setBatchNo(processIncomingInspect.getBatchNo());
            gpInspectionTable.setProductName(processIncomingInspect.getProductName());
            gpInspectionTable.setProductColor(processIncomingInspect.getProductColor());

            gpInspectionTables.add(gpInspectionTable);

        }
        return iGpInspectionTableService.saveBatch(gpInspectionTables);
    }

    @Override
    @Transactional
    public boolean createProductionProcessingTracking(GpProcessIncomingInspectUpdate gpProcessIncomingInspectUpdate) {
        ArrayList<GpProcessProblemTrack> gpProcessProblemTracks = new ArrayList<>();
        List<Long> ids = gpProcessIncomingInspectUpdate.getIds();
        for (Long id : ids) {
            GpProcessProblemTrack gpProcessProblemTrack = new GpProcessProblemTrack();
            GpProcessIncomingInspect processIncomingInspect = this.getById(id);
            processIncomingInspect.setTrackFlag(YesNoEnum.YES.getKey());
            this.updateById(processIncomingInspect);
            if (StringUtils.isNotBlank(gpProcessIncomingInspectUpdate.getCreateBy())) {
                gpProcessProblemTrack.setCreateBy(gpProcessIncomingInspectUpdate.getCreateBy());
            }
            gpProcessProblemTrack.setSn(processIncomingInspect.getSn());
            gpProcessProblemTrack.setIncomingInspectId(processIncomingInspect.getId());
            gpProcessProblemTrack.setProcessSchedulingId(processIncomingInspect.getProcessSchedulingId());

            gpProcessProblemTrack.setProcessor(processIncomingInspect.getProcessor());
            gpProcessProblemTrack.setProductName(processIncomingInspect.getProductName());
            gpProcessProblemTrack.setProductColor(processIncomingInspect.getProductColor());

            gpProcessProblemTrack.setBatchNo(processIncomingInspect.getBatchNo());
            gpProcessProblemTrack.setDocumentaryId(processIncomingInspect.getDocumentaryId());
            gpProcessProblemTrack.setDocumentaryMan(processIncomingInspect.getDocumentaryMan());
            gpProcessProblemTrack.setProcessDeliveryNoteId(processIncomingInspect.getProcessDeliveryNoteId());



            GpProcessScheduling processScheduling = iGpProcessSchedulingService.getById(processIncomingInspect.getProcessSchedulingId());
            gpProcessProblemTrack.setBackerName(processScheduling.getBackerName());
            gpProcessProblemTrack.setBackerColor(processScheduling.getBackerColor());


            gpProcessProblemTracks.add(gpProcessProblemTrack);

        }
        return iGpProcessProblemTrackService.saveBatch(gpProcessProblemTracks);
    }

    @Override
    public boolean updateByWeightAndWidthAndYardVerify(List<GpProcessIncomingInspectUpdate> gpIncomingInspectUpdateList) {
        for (GpProcessIncomingInspectUpdate gpIncomingInspectUpdate : gpIncomingInspectUpdateList) {
            this.editIncomingInspect(gpIncomingInspectUpdate);
            this.updateProcessDeliveryNote(gpIncomingInspectUpdate);
            //this.updateByWeightAndWidthAndYardVerifyOne();
        }
        return true;
    }

    @Transactional
    public void updateProcessDeliveryNote(GpProcessIncomingInspectUpdate gpIncomingInspectUpdate) {
        GpProcessDeliveryNote gpProcessDeliveryNote = iGpProcessDeliveryNoteService.getById(gpIncomingInspectUpdate.getId());
        gpProcessDeliveryNote.setProcessor(gpIncomingInspectUpdate.getProcessor());
        gpProcessDeliveryNote.setDyeVatNo(gpIncomingInspectUpdate.getBatchNo());
        gpProcessDeliveryNote.setProductName(gpIncomingInspectUpdate.getProductName());
        gpProcessDeliveryNote.setProductColor(gpIncomingInspectUpdate.getProductColor());
        gpProcessDeliveryNote.setTotalSizeEmpty(gpIncomingInspectUpdate.getTotalSizeEmpty());
        gpProcessDeliveryNote.setTotalSizeSufficient(gpIncomingInspectUpdate.getTotalSizeSufficient());
        gpProcessDeliveryNote.setCodingSpecificationsEmpty(gpIncomingInspectUpdate.getStandardCodeEmpty());
        gpProcessDeliveryNote.setCodingSpecificationsSufficient(gpIncomingInspectUpdate.getStandardCodeSufficient());
        iGpProcessDeliveryNoteService.updateById(gpProcessDeliveryNote);

        //更新排单表的数据
        GpProcessScheduling gpProcessScheduling = iGpProcessSchedulingService.getById(gpIncomingInspectUpdate.getProcessSchedulingId());
        gpProcessScheduling.setProcessor(gpIncomingInspectUpdate.getProcessor());
        gpProcessScheduling.setProductColor(gpIncomingInspectUpdate.getProductColor());
        gpProcessScheduling.setProductName(gpIncomingInspectUpdate.getProductName());
        gpProcessScheduling.setProcessor(gpIncomingInspectUpdate.getProcessor());
        gpProcessScheduling.setProductName(gpIncomingInspectUpdate.getProductName());
        gpProcessScheduling.setProductColor(gpIncomingInspectUpdate.getProductColor());
        iGpProcessSchedulingService.updateById(gpProcessScheduling);

    }

    @Override
    public boolean updateByWeightAndWidthAndYardVerifyOne(GpProcessIncomingInspectUpdate gpProcessIncomingInspectUpdate) {
        return  this.editIncomingInspect(gpProcessIncomingInspectUpdate);
    }

    @Override
    public boolean copy(Long id, Integer copyNum) {
        GpProcessIncomingInspect gpProcessIncomingInspect = this.getById(id);
        if (gpProcessIncomingInspect == null) {
            return false;
        }
        List<GpProcessIncomingInspectYardVerify> yardVerifyList = iGpProcessIncomingInspectYardVerifyService.list(new LambdaQueryWrapper<GpProcessIncomingInspectYardVerify>().eq(GpProcessIncomingInspectYardVerify::getDelStatus, YesNoEnum.NO.getKey()).eq(GpProcessIncomingInspectYardVerify::getIncomingInspectId, gpProcessIncomingInspect.getId()));
        List<GpProcessIncomingInspectFormaldehyde> formaldehydeList = iGpProcessIncomingInspectFormaldehydeService.list(new LambdaQueryWrapper<GpProcessIncomingInspectFormaldehyde>().eq(GpProcessIncomingInspectFormaldehyde::getDelStatus, YesNoEnum.NO.getKey()).eq(GpProcessIncomingInspectFormaldehyde::getIncomingInspectId, gpProcessIncomingInspect.getId()));
        List<GpProcessIncomingInspectWidth> widthList = iGpProcessIncomingInspectWidthService.list(new LambdaQueryWrapper<GpProcessIncomingInspectWidth>().eq(GpProcessIncomingInspectWidth::getDelStatus, YesNoEnum.NO.getKey()).eq(GpProcessIncomingInspectWidth::getIncomingInspectId, gpProcessIncomingInspect.getId()));
        for (int i = 0; i < copyNum; i++) {
            gpProcessIncomingInspect.setId(null);
            this.save(gpProcessIncomingInspect);
            // 复制加工来料检验单实际验码子表
            yardVerifyList.forEach(yardVerify -> {
                yardVerify.setId(null);
                yardVerify.setIncomingInspectId(gpProcessIncomingInspect.getId());
            });
            iGpProcessIncomingInspectYardVerifyService.saveBatch(yardVerifyList);
            // 复制加工来料检验单甲醛PM值子表
            formaldehydeList.forEach(formaldehyde -> {
                formaldehyde.setId(null);
                formaldehyde.setIncomingInspectId(gpProcessIncomingInspect.getId());
            });
            iGpProcessIncomingInspectFormaldehydeService.saveBatch(formaldehydeList);
            // 复制加工来料检验单幅宽子表
            widthList.forEach(width -> {
                width.setId(null);
                width.setIncomingInspectId(gpProcessIncomingInspect.getId());
            });
            iGpProcessIncomingInspectWidthService.saveBatch(widthList);
        }
        return true;
    }


    private boolean editIncomingInspect(GpProcessIncomingInspectUpdate gpIncomingInspectUpdate) {
        List<GpProcessIncomingInspectWidth> gpProcessIncomingInspectWidthList = gpIncomingInspectUpdate.getGpProcessIncomingInspectWidthList();
        for (GpProcessIncomingInspectWidth gpProcessIncomingInspectWidth : gpProcessIncomingInspectWidthList) {
            gpProcessIncomingInspectWidth.setIncomingInspectId(gpIncomingInspectUpdate.getId());
        }
        iGpProcessIncomingInspectWidthService.saveOrUpdateBatch(gpProcessIncomingInspectWidthList);


        List<GpProcessIncomingInspectYardVerify> gpProcessIncomingInspectYardVerifyList = gpIncomingInspectUpdate.getGpProcessIncomingInspectYardVerifyList();
        for (GpProcessIncomingInspectYardVerify gpProcessIncomingInspectYardVerify : gpProcessIncomingInspectYardVerifyList) {
            gpProcessIncomingInspectYardVerify.setIncomingInspectId(gpIncomingInspectUpdate.getId());
        }
        iGpProcessIncomingInspectYardVerifyService.saveOrUpdateBatch(gpProcessIncomingInspectYardVerifyList);


        List<GpProcessIncomingInspectFormaldehyde> gpProcessIncomingInspectFormaldehydeList = gpIncomingInspectUpdate.getGpProcessIncomingInspectFormaldehydeList();
        for (GpProcessIncomingInspectFormaldehyde gpProcessIncomingInspectFormaldehyde : gpProcessIncomingInspectFormaldehydeList) {
            gpProcessIncomingInspectFormaldehyde.setIncomingInspectId(gpIncomingInspectUpdate.getId());
        }
        iGpProcessIncomingInspectFormaldehydeService.saveOrUpdateBatch(gpProcessIncomingInspectFormaldehydeList);

        GpProcessIncomingInspect gpProcessIncomingInspect = Convert.convert(new TypeReference<GpProcessIncomingInspect>() {}, gpIncomingInspectUpdate);
        return this.updateById(gpProcessIncomingInspect);
    }
}
